﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VideoGiaitri.Models;
using Newtonsoft.Json;
using PagedList;
namespace VideoGiaitri.Controllers
{
    public class LmhtController : Controller
    {
        //
        // GET: /Lmht/
        tinviet_vietnamEntities db = new tinviet_vietnamEntities();
        public ActionResult Index(int? page)
        {
            ViewBag.title = "Video liên minh huyền thoại hay, mới, lạ";
            var p = (from q in db.topgames where q.typegame==2 select q).OrderByDescending(o => o.id);
            int pageSize = 50;
            int pageNumber = (page ?? 1);
            return View(p.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult Search(int? page, string keyword)
        {
            ViewBag.title = "Tìm kiếm video có chứa từ khóa " + keyword;
            ViewBag.keyword = keyword;
            var p = (from q in db.topgames where q.title.Contains(keyword) && q.typegame == 2 select q).OrderByDescending(o => o.id);
            int pageSize = 50;
            int pageNumber = (page ?? 1);
            return View(p.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult xem(int id)
        {
            topgame vd = db.topgames.Find(id);
            if (vd == null)
            {
                return HttpNotFound();
            }
            return View(vd);
        }
        public string getRelatedGame(int id)
        {
            var p = (from q in db.topgames where !q.id.Equals(id) && q.typegame == 2 select q).OrderByDescending(o => o.id).Take(10);
            return JsonConvert.SerializeObject(p.ToList());
        }
        public string getOldRelatedGame(int id)
        {
            var p = (from q in db.topgames where q.id < id && q.typegame == 2 select q).OrderByDescending(o => o.id).Take(9);
            return JsonConvert.SerializeObject(p.ToList());
        }
    }
}
